package com.ketangpai.admin.controller;

import com.ketangpai.admin.entity.User;


import com.ketangpai.admin.entity.dto.CourseDTO;
import com.ketangpai.admin.entity.dto.TopCourseDTO;
import com.ketangpai.admin.entity.param.*;
import com.ketangpai.admin.service.CourseService;
import com.ketangpai.admin.service.UserCourseService;
import com.ketangpai.admin.utils.JsonResult;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userCourse")
@Api(tags = "用户课程管理")
public class UserCourseController {
    private UserCourseService userCourseService;
    private CourseService courseService;

    public UserCourseController(UserCourseService userCourseService, CourseService courseService) {
        this.userCourseService = userCourseService;
        this.courseService = courseService;
    }
    @PostMapping("/list")
    public  JsonResult getList(@RequestBody GetUserCourseParam getUserCourseParam){
        return JsonResult.success(userCourseService.getList(getUserCourseParam));
    }

    @PostMapping("/topList")
    public JsonResult getTopCourse(@RequestBody User user){
        System.out.println("getTopCourse"+user.getId());
        List<TopCourseDTO> courses=userCourseService.getTopCourse(user);
        System.out.println("courses"+courses);
        return JsonResult.success(courses);
    }
    @PostMapping("/update")
    public JsonResult changeIsTopAndIsPigeonhole(@RequestBody ChangeParam changeParam){
        boolean result=userCourseService.changeIsTop(changeParam);
        if(result){
            return JsonResult.success("操作成功");
        }else {
            return JsonResult.error("操作失败");
        }
    }
    @PostMapping("/deleteCourse")
    public JsonResult deleteCourse(@RequestBody DropCourseParam dropCourseParam) throws Exception {

        String result=userCourseService.dropCourse(dropCourseParam);
        if(result.equals("退课成功")){
            return JsonResult.success("退课成功");
        }else if (result.equals("密码错误")){
            return JsonResult.error("密码错误");
        }else {
            return JsonResult.error("退课失败");
        }
    }

    @PostMapping("/join")
    public JsonResult join(@RequestBody JoinCourseParam joinCourseParam){
        String result=userCourseService.joinCourse(joinCourseParam);
        if(result.equals("已经加入该课程了")){
            return JsonResult.error("已经加入该课程了");
        }else if (result.equals("找不到课程")){
            return JsonResult.error("找不到课程");
        }else if (result.equals("加入课程成功")){
            return JsonResult.success("加入课程成功");
        }else {
            return JsonResult.error("加入失败");
        }
    }
    //根据type来获取课程
    @PostMapping("/getCourseByType")
    public JsonResult getCourses(@RequestBody CourseByTypeParam courseByTypeParam){
        Map<String,List<CourseDTO>> course=userCourseService.getCourseByType(courseByTypeParam);
        return JsonResult.success(course);
    }

    @PostMapping("/getPigeonholeCourse")
    public JsonResult getPigeonholeCourse(@RequestBody CourseByTypeParam courseByTypeParam){
        Map<String,List<CourseDTO>> course=userCourseService.getPigeonholeCourse(courseByTypeParam);
        return JsonResult.success(course);
    }

    @PostMapping("/getTeachers")
    public JsonResult getTeachers(@RequestBody Integer courseId){
        List<User> users=userCourseService.getTeachers(courseId);
        return JsonResult.success(users);
    }

    @PostMapping("/getStudents")
    public JsonResult getStudents(@RequestBody Integer courseId){
        List<User> users=userCourseService.getStudents(courseId);
        return JsonResult.success(users);
    }

    @PostMapping("/addMember")
    public JsonResult add(@RequestBody AddMemberParam addMemberParam){
        if(userCourseService.addTeacher(addMemberParam)){
            return JsonResult.success("添加成功");
        }else {
            return JsonResult.error("账号不存在");
        }
    }

    @PostMapping("/changeAll")
    public JsonResult changeIsPigeonholeAll(@RequestBody Integer courseId){
        if(userCourseService.changeIsPigeonholeAll(courseId))return JsonResult.success("归档成功");
        else return JsonResult.error("归档失败");
    }

    @PostMapping("/getUserType")
    public JsonResult getUserType(@RequestBody GetUserCourseParam getUserCourseParam){
        return JsonResult.success(userCourseService.getList(getUserCourseParam));
    }

    @PostMapping("/deleteStudent")
    public JsonResult deleteStudent(@RequestBody DeleteStudentParam deleteStudentParam){
        if(userCourseService.deleteStudent(deleteStudentParam)){
            return JsonResult.success("删除成功");

        }else return JsonResult.error("删除失败");
    }
}
